using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenXLive.Features;
using OpenXLive;
using OpenXLive.Form;
using System.Diagnostics;
using cocos2d;
using OpenXLive.Forms;

namespace CampaignWP7
{
    class SocialManager
    {
        public static readonly SocialManager instance = new SocialManager();
        private SocialManager() { }

        //init
        public void init()
        {
            
        }

        //social page
        public void LogInPage()
        {
            OpenXLive.Forms.XLiveFormFactory.Factory.ShowLoginPage();
        }

        public void SelectedOpenXLive()
        {
            CCDirector.sharedDirector().pushScene(ScoreLayer.scene());
        }

        public void SelectedLoginOpenXLive()
        {
            if (XLiveGameManager.CurrentSession != null && XLiveGameManager.CurrentSession.IsValid)
            {
                XLiveFormFactory.Factory.ShowLoginPage();
            }
        }

        public void SelectedGameCenter()
        {
            if (XLiveGameManager.CurrentSession != null && XLiveGameManager.CurrentSession.IsValid)
            {
                XLiveFormFactory.Factory.ShowGameCenterPage();
            }
        }

        public void SelectedLeaderBoard()
        {
            if (OpenXLive.Network.IsConnect == false)
            {
                MessageBox.Show("Network is not connect, please connect network first");
            }
            if (XLiveGameManager.CurrentSession != null && XLiveGameManager.CurrentSession.IsValid)
            {
                XLiveFormFactory.Factory.ShowLeaderboardPage();
            }
        }

        public void SelectedAchievement(object sender)
        {
            if (OpenXLive.Network.IsConnect == false)
            {
                MessageBox.Show("Network is not connect, please connect network first");
            }
            if (XLiveGameManager.CurrentSession != null && XLiveGameManager.CurrentSession.IsValid)
            {
                XLiveFormFactory.Factory.ShowAchievementsPage();
            }

        }
        //score
        private string killWolfKey = "bb4bab4c-dc1a-4609-a748-0c368a0e02aa";
        public void UploadTheNumberOfWolvesKilled(int value)
        {
            Leaderboard lb = new Leaderboard(XLiveGameManager.CurrentSession, killWolfKey);

            lb.SubmitScoreCompleted += new AsyncEventHandler(lb_SubmitKillWolf);

            lb.SubmitScore(value);
        }

        void lb_SubmitKillWolf(object sender, AsyncEventArgs e)
        {
            if (e.Result.ReturnValue)
            {
                //succeed
            }
            else
            {
                MessageBox.Show(e.Result.ErrorMessage);
            }
        }

        //Achievement
        public enum GameAchievements
        {
            Achievement_KillWolvesManster = 0,
        };
        private string Achievement_KillWolvesMasterKey = "";
        public void GetAchievement(GameAchievements achievementID)
        {
            string getAchievementStr = "";

            switch (achievementID)
            {
                case GameAchievements.Achievement_KillWolvesManster:
                    getAchievementStr = Achievement_KillWolvesMasterKey;
                    break;
            }

            Achievement item = new Achievement(XLiveGameManager.CurrentSession, getAchievementStr);
            item.AwardCompleted += new AsyncEventHandler(achievement_AwardCompleted);
            item.Award();
        }
        void achievement_AwardCompleted(object sender, AsyncEventArgs e)
        {
            if (e.Result.ReturnValue)
            {
                // Success
            }
            else
            {
                Debug.WriteLine(e.Result.ErrorMessage);
            }
        }

    }
}
